<template>
  <div class="commitGraduation">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>毕业设计</el-breadcrumb-item>
      <el-breadcrumb-item>提交毕业设计结果</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form ref="courseTitleForm" :model="courseTitleForm" label-width="80px" class="content fixed-form">

      <el-form-item label="题目名称">
        <el-input v-model="courseTitleForm.graduateTitle"></el-input>
      </el-form-item>
      <el-form-item label="负责教师">
        <el-input v-model="courseTitleForm.teacherName"></el-input>
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="courseTitleForm.uploadTime"
          @change="setTime"
          style="width: 100%;">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="毕业论文">
        <el-upload
          ref="upload1"
          :action="this.$until.url +'/graduateResult/uploadDissertation'"
          :auto-upload="false"
          :on-success="setUploadDissertation"
          name="dissertationName">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload1">上传到服务器</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="毕业成果">
        <el-upload
          ref="upload2"
          :action="this.$until.url +'/graduateResult/uploadGraduateResult'"
          :auto-upload="false"
          :on-success="setUploadGraduateResult"
          name=" graduateResultName">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload2">上传到服务器</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="毕设介绍">
        <el-input
          type="textarea"
          :rows="5"
          placeholder="请输入内容"
          v-model="courseTitleForm.remark1">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button>取消</el-button>
        <el-button type="primary" @click="onCheck">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default{
    name: 'courseTitle',
    data(){
      return {
        courseTitleForm: {
          graduateProgramID: '',
          studentID: '',
          graduateTitle: '',
          teacherName: '',
          graduateResult: '',
          dissertation: '',
          graduateResultName: '',
          graduateResultStore: '',
          dissertationName: '',
          dissertationStore: '',
          uploadTime: '',
          remark1: ''
        },
        apiUrl: this.$until.url + '/graduateResult/addGraduateResult',
        initUrl: this.$until.url + '/graduateResult/getStudentGraduate',
        submitUrl: this.$until.url + '/graduateResult/updateCheck'
      }
    },
    mounted: function () {
      this.$nextTick(function () {
        this.init();
      });
    },
    methods: {
      init(){
        var msg = this.$route.query;
        for (var key in msg) {
          this.courseTitleForm[key] = msg[key]
        }
        this.res()
      },
      res(){
        this.$http.get(this.initUrl, {
            params: {
              studentID: this.courseTitleForm.studentID
            }
          }
        ).then((res) => {
          var msg = res.body;
          for (var key in msg) {
            this.courseTitleForm[key] = msg[key]
          }
        }, (err) => {
          console.log(err);
        })
      },
      onSubmit: function () {
        this.$http.post(this.apiUrl, {
            params: {
              studentID: this.courseTitleForm.studentID
            }
          })
          .then((res) => {
            this.msg = res.body.message;
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
          }, (err) => {
            this.$notify.error({
              title: '错误',
              message: '保存失败'
            });
          })
      },
      onCheck: function () {

      	this.$confirm('确认提交后将不可在此更改, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(this.submitUrl, this.courseTitleForm, {emulateJSON: true})
          .then((res) => {
            this.msg = res.body.message;
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success'
            });
          }, (err) => {
            this.$notify.error({
              title: '错误',
              message: '提交失败'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消提交'
          });
        });
      },
      submitUpload1() {
        this.$refs.upload1.submit();
      },
      submitUpload2() {
        this.$refs.upload2.submit();
      },
      setUploadDissertation(response, file, fileList){
        for (var key in response) {
          this.courseTitleForm[key] = response[key]
        }
      },
      setUploadGraduateResult(response, file, fileList){
        for (var key in response) {
          this.courseTitleForm[key] = response[key]
        }
      },
      setTime(val){
        this.courseTitleForm.uploadTime = val
      }
    }
  }
</script>
<style>

</style>
